/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web.servlet;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author KenDa
 */
public class MySaxParser extends DefaultHandler {

    private String qname;
    private String searchValue;
    private boolean flag;
    private String password;
    private boolean Found;

    public MySaxParser() {
        qname = "";
        flag = false;
        Found = false;
    }

    public MySaxParser(String searchValue) {
        this.searchValue = searchValue;
        qname = "";
        flag = false;
        Found = false;
        System.out.println("Create MySaxParser with searchValue: " + searchValue);
    }

    public MySaxParser(String ten, String matkhau) {
        qname = "";
        flag = false;
        Found = false;
        searchValue = ten;
        password = matkhau;
        System.out.println("Create MySaxParser with ten and matkhau: " + ten + ", " + matkhau);
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        System.out.println("uri: " + uri);
        System.out.println("localname: " + localName);
        System.out.println("qname: " + qName);
        System.out.println("att: " + attributes);
        this.qname = qName;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String str = new String(ch, start, length);
        System.out.println("str characters: "+str);
        if (Found == false) {
            if (qname.equals("ten")) {
                String str1 = new String(ch, start, length);
                System.out.println("ten: "+str1);
                if (str1.trim().equals(searchValue)) {
                    flag = true;
                }
            }

            if (flag == true) {
                if (qname.equals("matkhau")) {
                    String str2 = new String(ch, start, length);
                    System.out.println("matkhau: "+str2);
                    if (str2.trim().equals(password)) {
                        Found = true;
                        System.out.println("Found");
                    }else
                    {
                        flag = false;
                        //Không kiểm tra mật khẩu tiếp nữa
                    }
                }
            }
        }

    }

    public String getPassword() {
        return password;
    }

    public boolean isFound() {
        return Found;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        this.qname = "";
    }
}
